'use strict';
module.exports = {
	/**
	 * 基于字段的统计
	 * @url admin/pages/kh/tjsj/tglj/fields_agg 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			colums
		} = data;
		let res = {
			code: 0,
			msg: ""
		};

		console.log("data", data)
		if (!colums || colums.lenght === 0) {
			res.code = 1;
			res.msg = "字段不能为空";
			return res
		}

		// 业务逻辑开始-----------------------------------------------------------
		const $ = db.command.aggregate;

		const group = {
			_id: "-"
		};
		for (let column of colums) {
			const columnName = column.columnName
			switch (column.type) {
				case "sum":
					group[columnName] = $.sum("$" + columnName)
					break
				case "avg":
					group[columnName] = $.avg("$" + columnName)
					break
				default:
					res.code = 1
					res.msg = "未知的算法: " + column.type
					return res;
			}
		}

		res.data = await db.collection("a_dy_tfurl").aggregate().group(group).end()

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}